package org.example.Soution002;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class letterComb {
    public static void main(String[] args) {
        letterComb letterComb = new letterComb();
        System.out.println(letterComb.letterCombinations("2"));
    }

    public enum PhoneKey{
        KEY_2('2',"abc"),
        KEY_3('3',"def"),
        KEY_4('4',"ghi"),
        KEY_5('5',"jkl"),
        KEY_6('6',"mno"),
        KEY_7('7',"pqrs"),
        KEY_8('8',"tuv"),
        KEY_9('9',"wxyz");

        private final char number;
        private final String letters;

        PhoneKey(char number, String letters) {
            this.number=number;
            this.letters=letters;
        }

        public char getNumber() {return number;}
        public String getLetters() {return letters;}
    }

    public static PhoneKey getPhoneKey(char number) {
        for(PhoneKey phoneKey:PhoneKey.values()){
            if(phoneKey.getNumber()==number){
                return phoneKey;
            }
        }
        return null;
    }

    List<String> result = new ArrayList<>();
    public List<String> letterCombinations(String digits) {
        char[] chars = digits.toCharArray();
        if(chars.length==0){
            return new ArrayList<>();
        }
        letterCombine(chars,digits.length(),0,"");
        return result;
    }

    public void letterCombine(char[] chars, int length, int position,String letters) {
        if(position==length){
            result.add(letters);
            return;
        }
        String letter = getPhoneKey(chars[position]).getLetters();
        char[] newChars = letter.toCharArray();
        for(char c:newChars){
            letterCombine(chars,length,position+1,letters+c);
        }
    }
}
